package com.bytz.modules.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bytz.common.api.vo.Result;
import com.bytz.common.system.base.controller.BytzController;
import com.bytz.common.util.oConvertUtils;
import com.bytz.modules.system.entity.SysDepartPermission;
import com.bytz.modules.system.entity.SysPermissionDataRule;
import com.bytz.modules.system.service.ISysDepartPermissionService;
import com.bytz.modules.system.service.ISysPermissionDataRuleService;
import com.bytz.modules.system.service.ISysPermissionService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @Description: 部门权限表
* @Author: bytz-boot
* @Date:   2020-02-11
* @Version: V1.0
*/
@Slf4j
@Api(tags="部门权限表")
@RestController
@RequestMapping("/sys/sysDepartPermission")
public class SysDepartPermissionController extends BytzController<SysDepartPermission, ISysDepartPermissionService> {
    @Autowired
    private ISysDepartPermissionService sysDepartPermissionService;

    @Autowired
    private ISysPermissionDataRuleService sysPermissionDataRuleService;

    @Autowired
    private ISysPermissionService sysPermissionService;

    /**
     * 部门管理授权查询数据规则数据
     */
    @GetMapping(value = "/datarule/{permissionId}/{departId}")
    public Result<?> loadDatarule(@PathVariable("permissionId") String permissionId, @PathVariable("departId") String departId) {
        List<SysPermissionDataRule> list = sysPermissionDataRuleService.getPermRuleListByPermId(permissionId);
        if (list == null || list.size() == 0) {
            return Result.error("未找到权限配置信息");
        } else {
            Map<String, Object> map = new HashMap<>();
            map.put("datarule", list);
            LambdaQueryWrapper<SysDepartPermission> query = new LambdaQueryWrapper<SysDepartPermission>()
                    .eq(SysDepartPermission::getPermissionId, permissionId)
                    .eq(SysDepartPermission::getDepartId, departId);
            SysDepartPermission sysDepartPermission = sysDepartPermissionService.getOne(query);
            if (sysDepartPermission == null) {
                //return Result.error("未找到角色菜单配置信息");
            } else {
                String drChecked = sysDepartPermission.getDataRuleIds();
                if (oConvertUtils.isNotEmpty(drChecked)) {
                    map.put("drChecked", drChecked.endsWith(",") ? drChecked.substring(0, drChecked.length() - 1) : drChecked);
                }
            }
            return Result.ok(map);
            //TODO 以后按钮权限的查询也走这个请求 无非在map中多加两个key
        }
    }

    /**
     * 保存数据规则至部门菜单关联表
     */
    @PostMapping(value = "/datarule")
    public Result<?> saveDatarule(@RequestBody JSONObject jsonObject) {
        try {
            String permissionId = jsonObject.getString("permissionId");
            String departId = jsonObject.getString("departId");
            String dataRuleIds = jsonObject.getString("dataRuleIds");
            log.info("保存数据规则>>" + "菜单ID:" + permissionId + "部门ID:" + departId + "数据权限ID:" + dataRuleIds);
            LambdaQueryWrapper<SysDepartPermission> query = new LambdaQueryWrapper<SysDepartPermission>()
                    .eq(SysDepartPermission::getPermissionId, permissionId)
                    .eq(SysDepartPermission::getDepartId, departId);
            SysDepartPermission sysDepartPermission = sysDepartPermissionService.getOne(query);
            if (sysDepartPermission == null) {
                return Result.error("请先保存部门菜单权限!");
            } else {
                sysDepartPermission.setDataRuleIds(dataRuleIds);
                this.sysDepartPermissionService.updateById(sysDepartPermission);
            }
        } catch (Exception e) {
            log.error("SysDepartPermissionController.saveDatarule()发生异常：" + e.getMessage(), e);
            return Result.error("保存失败");
        }
        return Result.ok("保存成功!");
    }

}
